import {
  Controller,
  Get,
  Post,
  Body,
  Header,
  Query,
  UseGuards,
  Request,
} from '@nestjs/common';
import { UsersService } from './users.service';
import { AuthGuard } from '@nestjs/passport';

@Controller('users')
export class UsersController {
  constructor(private readonly usersService: UsersService) {}
  //登录
  @UseGuards(AuthGuard('local'))
  @Post('/login')
  async login(@Body() param, @Request() req) {
    return await this.usersService.login(req.user);
  }

  @UseGuards(AuthGuard('jwt'))
  @Post('/loginOut')
  async loginOut(@Request() req) {
    return await this.usersService.loginOut(req.user);
  }

  //邮箱验证码
  @Post('/getRegisterCaptcha')
  async getCaptcha(@Body() param) {
    return await this.usersService.getRegisterCaptcha(param);
  }

  //邮箱注册
  @Post('/register')
  async register(@Body() param) {
    return await this.usersService.register(param);
  }
  //获取用户信息
  @UseGuards(AuthGuard('jwt'))
  @Get('/currentUser')
  async currentUser(@Request() req) {
    return await this.usersService.getUser(req.user);
    // return await this.usersService.findUser(req.user);
  }
  //获取用户信息表格
  @UseGuards(AuthGuard('jwt'))
  @Post('/getUserList')
  async getUserList(@Body() param) {
    return await this.usersService.getUserList(param);
  }
  //新建保存用户
  @UseGuards(AuthGuard('jwt'))
  @Post('/saveUser')
  async saveUser(@Body() param) {
    return await this.usersService.saveUser(param);
  }
  //获取指定用户
  @Get('/getUser')
  async getUser(@Query() param) {
    return await this.usersService.getUser(param);
  }
  //批量删除角色
  @Post('/delUserList')
  async delUserList(@Body() param) {
    return await this.usersService.delUserList(param);
  }
  //====================下为模板接口========================
  @Get()
  findAll() {
    return this.usersService.findAll();
  }

  @Post()
  async create(@Body() param) {
    const newParam = { ...param, status: true };
    await this.usersService.create(newParam);
    return true;
  }

  @Post('/many')
  async createMany(@Body() users) {
    const newUsers = users.map(user => ({ ...user, status: true }));
    await this.usersService.createMany(newUsers);
    return true;
  }
}
